<?php

namespace api\controllers;

use api\models\ActiveGoodForm;
use api\models\AstatusForm;
use yii\web\Response;

class AstatusController extends \yii\web\Controller
{
    /**
     * 检查 活动商品是否有效
     * http://api.spike.com/astatus/2_3.js
     *
     * http://api.spike.com/?r=astatus/index&aid=2&gid=3
     */
    public function actionIndex()
    {
        \Yii::$app->response->format = Response::FORMAT_JSON;

        $req_param = \Yii::$app->request->get();

        $active_id = intval( $req_param[ 'aid' ] );
        $good_id = intval( $req_param[ 'gid' ] );

        if( !$active_id || !$good_id )
        {
            $ret2response = [ 'status' => false,'msg' => '请求参数异常' ];
            return \Yii::$app->response->data = $ret2response;
        }

        $act_good_info = \Yii::$app->redis->mget( 'st_a_' . $active_id, 'st_g_' . $good_id );

        $astatus_form = new AstatusForm();
        if ( $act_good_info && $act_good_info[0] && $act_good_info[1] ) {

            // 活动状态和商品状态都是正常状态，才可以返回一个正确的验证码
            $info2sign = array(
                'now' => time(),
                'ip' => $astatus_form -> getClientIp(),
                'aid' => $active_id,
                'gid' => $good_id,
                'uid' => \Yii::$app->user->id,
            );

            // 在这个位置设置一个可以加密&可以解密,但不可串改的密钥
            $str = $astatus_form -> rc_encrypt( $info2sign,'E' );

            return \Yii::$app->response->data = [ 'user_sign' => $str ];
        }

        //设置售罄信息
        return \Yii::$app->response->data = $astatus_form -> soldOutJsSetting( $active_id, $good_id );
    }

}
